package com.llt.chatroom.config;

import com.llt.chatroom.controller.WebSocketController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private WebSocketController webSocketController;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 将创建好的 Handler 类注册到具体的路径上
        // 此时当浏览器 websocket 的请求路径是"/WebSocketMessag"的时候，就会调用 WebSocketController 里的方法
        registry.addHandler(webSocketController, "/WebSocketMessage")
                // 通过这个拦截器，获取到 HttpSession
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }

}
